package com.pdstudio.plistview;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.pdstudio.apapter.SingleStationStateListAdapter;
import com.pdstudio.bean.MyListView;
import com.pdstudio.bean.MyStationState;
import com.pdstudio.bean.StationState;
import com.pdstudio.util.ViewUtil;
import com.pdstudio.weather.R;

public class NewStationStateListView extends ListView {

	// 所有获取到的数据
	private List<MyStationState> mStationStates = new ArrayList<MyStationState>();
	// 适配器
	private NewStationStateAdapter mNewStationStateAdapter;

	private Context mContext;

	public NewStationStateListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;

		// 关联GridView和Adapter
		mNewStationStateAdapter = new NewStationStateAdapter();
		this.setAdapter(mNewStationStateAdapter);
	}

	public void updateData(List<MyStationState> list) {
		mStationStates = list;

		// 重新设置数据源
		this.setAdapter(mNewStationStateAdapter);		
	}

	private void setListViewHeightBasedOnChildren(ListView listView) {
		ListAdapter listAdapter = listView.getAdapter();
		if (listAdapter == null) {
			return;
		}
		int totalHeight = 0;
		for (int i = 0; i < listAdapter.getCount(); i++) {
			View listItem = listAdapter.getView(i, null, listView);
			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight();
		}

		ViewGroup.LayoutParams params = listView.getLayoutParams();
		params.height = totalHeight
				+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
		listView.setLayoutParams(params);
	}

	class NewStationStateAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return mStationStates.size();
		}

		@Override
		public Object getItem(int pos) {
			// TODO Auto-generated method stub
			return mStationStates.get(pos);
		}

		@Override
		public long getItemId(int pos) {
			return 0;
		}

		@Override
		public View getView(int position, View v, ViewGroup parent) {
			// TODO Auto-generated method stub

			MyListView holder;

			if (v == null) {
				v = ViewUtil.getView(mContext,
						R.layout.newstation_state_listview_item);
				holder = new MyListView();

				// 从容器中获取一个MyListView
				holder.mAddress = (TextView) v.findViewById(R.id.state_text);
				holder.mListView = (ListView) v
						.findViewById(R.id.state_listview);
				v.setTag(holder);

			} else {
				holder = (MyListView) v.getTag();
			}

			holder.mAddress.setText(mStationStates.get(position).getAddress());
			// TODO:根据地方获取到data列表
			SingleStationStateListAdapter adapter = new SingleStationStateListAdapter(
					mContext, mStationStates.get(position)
							.getStationStateList());
			holder.mListView.setAdapter(adapter);
			setListViewHeightBasedOnChildren(holder.mListView);

			return v;
		}
	}
}
